from rest_framework import serializers
from ..models.problem_model import ProblemModel, TagModel, TypeModel

class TagSerializer(serializers.ModelSerializer):
    class Meta:
        model = TagModel
        fields = ['id', 'name']

class TypeSerializer(serializers.ModelSerializer):
    class Meta:
        model = TypeModel
        fields = ['id', 'name']

class ProblemListQuerySerializer(serializers.Serializer):
    """题目列表查询参数序列化器"""
    page = serializers.IntegerField(default=1, min_value=1)
    page_size = serializers.IntegerField(default=10, min_value=1, max_value=100)
    search = serializers.CharField(required=False, allow_blank=True)
    difficulty = serializers.ChoiceField(
        choices=['Easy', 'Medium', 'Hard'],
        required=False
    )
    tag_id = serializers.IntegerField(required=False)
    type_id = serializers.IntegerField(required=False)
    ordering = serializers.CharField(required=False)

class ProblemListSerializer(serializers.ModelSerializer):
    """题目列表序列化器"""
    tag = TagSerializer()
    type = TypeSerializer()
    pass_rate = serializers.FloatField(read_only=True)
    source = serializers.SerializerMethodField()
    difficulty = serializers.SerializerMethodField()

    class Meta:
        model = ProblemModel
        fields = [
            'id', 'title', 'difficulty', 'source',
            'pass_rate', 'total_submissions', 'accepted_submissions',
            'tag', 'type', 'create_time', 'update_time'
        ]
    
    def get_source(self, obj):
        return f"{obj.type.name} - {obj.tag.name}"

    def get_difficulty(self, obj):
        if obj.type.name == '简单':
            return 'Easy'
        elif obj.type.name == '中等':
            return 'Medium'
        elif obj.type.name == '困难':
            return 'Hard'

class ProblemDetailSerializer(serializers.ModelSerializer):
    """题目详情序列化器"""
    tag = TagSerializer()
    type = TypeSerializer()
    pass_rate = serializers.FloatField(read_only=True)
    source = serializers.SerializerMethodField()

    class Meta:
        model = ProblemModel
        fields = [
            'id', 'title', 'problem_description',
            'input_description', 'output_description',
            'difficulty', 'source', 'pass_rate',
            'total_submissions', 'accepted_submissions',
            'time_limit', 'memory_limit',
            'tag', 'type', 'create_time', 'update_time'
        ]
    
    def get_source(self, obj):
        return f"{obj.type.name} - {obj.tag.name}"

class ProblemCreateSerializer(serializers.ModelSerializer):
    """创建题目序列化器"""
    class Meta:
        model = ProblemModel
        fields = [
            'title', 'problem_description',
            'input_description', 'output_description',
            'difficulty', 'time_limit', 'memory_limit',
            'tag', 'type', 'answer'
        ]

class ProblemUpdateSerializer(serializers.ModelSerializer):
    """更新题目序列化器"""
    id = serializers.IntegerField(required=True)

    class Meta:
        model = ProblemModel
        fields = [
            'id', 'title', 'problem_description',
            'input_description', 'output_description',
            'difficulty', 'time_limit', 'memory_limit',
            'tag', 'type', 'answer'
        ]

class ProblemSerializer(serializers.Serializer):
    id = serializers.IntegerField()
    title = serializers.CharField(max_length=255)
    difficulty = serializers.ChoiceField(choices=['Easy', 'Medium', 'Hard'])
    type = serializers.IntegerField()
    tag = serializers.IntegerField()
    problem_description = serializers.CharField()
    input_description = serializers.CharField()
    output_description = serializers.CharField()
    answer = serializers.CharField()
    time_limit = serializers.IntegerField()
    memory_limit = serializers.IntegerField()